package com.xjh.mapper;

import static com.xjh.mapper.StudentDynamicDynamicSqlSupport.*;
import static org.mybatis.dynamic.sql.SqlBuilder.*;

import com.xjh.po.StudentDynamic;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

import com.xjh.po.xjhTable;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.dynamic.sql.BasicColumn;
import org.mybatis.dynamic.sql.delete.DeleteDSLCompleter;
import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider;
import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider;
import org.mybatis.dynamic.sql.insert.render.MultiRowInsertStatementProvider;
import org.mybatis.dynamic.sql.select.CountDSLCompleter;
import org.mybatis.dynamic.sql.select.SelectDSLCompleter;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
import org.mybatis.dynamic.sql.update.UpdateDSL;
import org.mybatis.dynamic.sql.update.UpdateDSLCompleter;
import org.mybatis.dynamic.sql.update.UpdateModel;
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
import org.mybatis.dynamic.sql.util.SqlProviderAdapter;
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils;

@Mapper
public interface StudentDynamicMapper {
    BasicColumn[] selectList = BasicColumn.columnList(id, stuName, stuSex, stuBirthday, classId, isEnable, createBy, createTime, updateBy, updateTime);

    @SelectProvider(type=SqlProviderAdapter.class, method="select")
    long count(SelectStatementProvider selectStatement);

    @DeleteProvider(type=SqlProviderAdapter.class, method="delete")
    int delete(DeleteStatementProvider deleteStatement);

    @InsertProvider(type=SqlProviderAdapter.class, method="insert")
    int insert(InsertStatementProvider<StudentDynamic> insertStatement);

    @InsertProvider(type=SqlProviderAdapter.class, method="insertMultiple")
    int insertMultiple(MultiRowInsertStatementProvider<StudentDynamic> multipleInsertStatement);

    @SelectProvider(type=SqlProviderAdapter.class, method="select")
    @ResultMap("StudentDynamicResult")
    Optional<StudentDynamic> selectOne(SelectStatementProvider selectStatement);

    @SelectProvider(type=SqlProviderAdapter.class, method="select")
    @Results(id="StudentDynamicResult", value = {
        @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
        @Result(column="stu_name", property="stuName", jdbcType=JdbcType.VARCHAR),
        @Result(column="stu_sex", property="stuSex", jdbcType=JdbcType.VARCHAR),
        @Result(column="stu_birthday", property="stuBirthday", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="class_id", property="classId", jdbcType=JdbcType.BIGINT),
        @Result(column="is_enable", property="isEnable", jdbcType=JdbcType.INTEGER),
        @Result(column="create_by", property="createBy", jdbcType=JdbcType.BIGINT),
        @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="update_by", property="updateBy", jdbcType=JdbcType.BIGINT),
        @Result(column="update_time", property="updateTime", jdbcType=JdbcType.TIMESTAMP)
    })
    List<StudentDynamic> selectMany(SelectStatementProvider selectStatement);

    @UpdateProvider(type=SqlProviderAdapter.class, method="update")
    int update(UpdateStatementProvider updateStatement);

    default long count(CountDSLCompleter completer) {
        return MyBatis3Utils.countFrom(this::count, studentDynamic, completer);
    }

    default int delete(DeleteDSLCompleter completer) {
        return MyBatis3Utils.deleteFrom(this::delete, studentDynamic, completer);
    }

    default int deleteByPrimaryKey(Long id_) {
        return delete(c -> 
            c.where(id, isEqualTo(id_))
        );
    }

    default int insert(StudentDynamic record) {
        return MyBatis3Utils.insert(this::insert, record, studentDynamic, c ->
            c.map(id).toProperty("id")
            .map(stuName).toProperty("stuName")
            .map(stuSex).toProperty("stuSex")
            .map(stuBirthday).toProperty("stuBirthday")
            .map(classId).toProperty("classId")
            .map(isEnable).toProperty("isEnable")
            .map(createBy).toProperty("createBy")
            .map(createTime).toProperty("createTime")
            .map(updateBy).toProperty("updateBy")
            .map(updateTime).toProperty("updateTime")
        );
    }

    default int insertMultiple(Collection<StudentDynamic> records) {
        return MyBatis3Utils.insertMultiple(this::insertMultiple, records, studentDynamic, c ->
            c.map(id).toProperty("id")
            .map(stuName).toProperty("stuName")
            .map(stuSex).toProperty("stuSex")
            .map(stuBirthday).toProperty("stuBirthday")
            .map(classId).toProperty("classId")
            .map(isEnable).toProperty("isEnable")
            .map(createBy).toProperty("createBy")
            .map(createTime).toProperty("createTime")
            .map(updateBy).toProperty("updateBy")
            .map(updateTime).toProperty("updateTime")
        );
    }

    default int insertSelective(StudentDynamic record) {
        return MyBatis3Utils.insert(this::insert, record, studentDynamic, c ->
            c.map(id).toPropertyWhenPresent("id", record::getId)
            .map(stuName).toPropertyWhenPresent("stuName", record::getStuName)
            .map(stuSex).toPropertyWhenPresent("stuSex", record::getStuSex)
            .map(stuBirthday).toPropertyWhenPresent("stuBirthday", record::getStuBirthday)
            .map(classId).toPropertyWhenPresent("classId", record::getClassId)
            .map(isEnable).toPropertyWhenPresent("isEnable", record::getIsEnable)
            .map(createBy).toPropertyWhenPresent("createBy", record::getCreateBy)
            .map(createTime).toPropertyWhenPresent("createTime", record::getCreateTime)
            .map(updateBy).toPropertyWhenPresent("updateBy", record::getUpdateBy)
            .map(updateTime).toPropertyWhenPresent("updateTime", record::getUpdateTime)
        );
    }

    default Optional<StudentDynamic> selectOne(SelectDSLCompleter completer) {
        return MyBatis3Utils.selectOne(this::selectOne, selectList, studentDynamic, completer);
    }

    default List<StudentDynamic> select(SelectDSLCompleter completer) {
        return MyBatis3Utils.selectList(this::selectMany, selectList, studentDynamic, completer);
    }

    default List<StudentDynamic> selectDistinct(SelectDSLCompleter completer) {
        return MyBatis3Utils.selectDistinct(this::selectMany, selectList, studentDynamic, completer);
    }

    default Optional<StudentDynamic> selectByPrimaryKey(Long id_) {
        return selectOne(c ->
            c.where(id, isEqualTo(id_))
        );
    }

    default int update(UpdateDSLCompleter completer) {
        return MyBatis3Utils.update(this::update, studentDynamic, completer);
    }

    static UpdateDSL<UpdateModel> updateAllColumns(StudentDynamic record, UpdateDSL<UpdateModel> dsl) {
        return dsl.set(id).equalTo(record::getId)
                .set(stuName).equalTo(record::getStuName)
                .set(stuSex).equalTo(record::getStuSex)
                .set(stuBirthday).equalTo(record::getStuBirthday)
                .set(classId).equalTo(record::getClassId)
                .set(isEnable).equalTo(record::getIsEnable)
                .set(createBy).equalTo(record::getCreateBy)
                .set(createTime).equalTo(record::getCreateTime)
                .set(updateBy).equalTo(record::getUpdateBy)
                .set(updateTime).equalTo(record::getUpdateTime);
    }

    static UpdateDSL<UpdateModel> updateSelectiveColumns(StudentDynamic record, UpdateDSL<UpdateModel> dsl) {
        return dsl.set(id).equalToWhenPresent(record::getId)
                .set(stuName).equalToWhenPresent(record::getStuName)
                .set(stuSex).equalToWhenPresent(record::getStuSex)
                .set(stuBirthday).equalToWhenPresent(record::getStuBirthday)
                .set(classId).equalToWhenPresent(record::getClassId)
                .set(isEnable).equalToWhenPresent(record::getIsEnable)
                .set(createBy).equalToWhenPresent(record::getCreateBy)
                .set(createTime).equalToWhenPresent(record::getCreateTime)
                .set(updateBy).equalToWhenPresent(record::getUpdateBy)
                .set(updateTime).equalToWhenPresent(record::getUpdateTime);
    }

    default int updateByPrimaryKey(StudentDynamic record) {
        return update(c ->
            c.set(stuName).equalTo(record::getStuName)
            .set(stuSex).equalTo(record::getStuSex)
            .set(stuBirthday).equalTo(record::getStuBirthday)
            .set(classId).equalTo(record::getClassId)
            .set(isEnable).equalTo(record::getIsEnable)
            .set(createBy).equalTo(record::getCreateBy)
            .set(createTime).equalTo(record::getCreateTime)
            .set(updateBy).equalTo(record::getUpdateBy)
            .set(updateTime).equalTo(record::getUpdateTime)
            .where(id, isEqualTo(record::getId))
        );
    }

    default int updateByPrimaryKeySelective(StudentDynamic record) {
        return update(c ->
            c.set(stuName).equalToWhenPresent(record::getStuName)
            .set(stuSex).equalToWhenPresent(record::getStuSex)
            .set(stuBirthday).equalToWhenPresent(record::getStuBirthday)
            .set(classId).equalToWhenPresent(record::getClassId)
            .set(isEnable).equalToWhenPresent(record::getIsEnable)
            .set(createBy).equalToWhenPresent(record::getCreateBy)
            .set(createTime).equalToWhenPresent(record::getCreateTime)
            .set(updateBy).equalToWhenPresent(record::getUpdateBy)
            .set(updateTime).equalToWhenPresent(record::getUpdateTime)
            .where(id, isEqualTo(record::getId))
        );
    }
//ResultMap只有一个属性，就是results的id。如果属性一致，不写result也没问题（result相当于resultMap,不写的话就相当于resultType了）。
    @ResultMap("StudentDynamicResult")
    @Select("select * from student_dynamic where id=#{id}")
    StudentDynamic findById(Long id);
}